package com.suxin.tomcat;

import io.netty.util.concurrent.DefaultThreadFactory;

import java.util.concurrent.*;

/**
 * @author Tang
 * @classname AbstractEndPoint
 * @description [  ]
 * @date 2021/7/3 15:27
 */
public abstract class AbstractEndPoint {

     public static class Acceptor implements Runnable{

        @Override
        public void run() {

        }

    }

    public static class PollerEvent implements Runnable{

        @Override
        public void run() {

        }
    }

    public static class Poller implements Runnable{

        @Override
        public void run() {

        }
    }


    public abstract void bind();

    private Executor executor;
    private int minSpareThreads = 10;
    private int maxThreads = 200;

    public void createExecutor() {
        executor = new ThreadPoolExecutor(minSpareThreads, maxThreads, 60,
                TimeUnit.SECONDS, new LinkedBlockingQueue<Runnable>(), new DefaultThreadFactory("tomcat-exec-"));
    }

    public Executor getExecutor() {
        return executor;
    }

    private int maxConnections = 10000;
    private Semaphore semaphore;

    public int getMaxConnections() {
        return maxConnections;
    }

    protected Semaphore initializeConnectionLatch() {
        if (semaphore == null) {
            semaphore = new Semaphore(getMaxConnections());
        }
        return semaphore;
    }

    protected void getAcquire() {
        try {
            semaphore.acquire();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    protected void releaseConnectionLatch() {
        semaphore.release();
    }

}